package com.unitd.frame.sso.web.waf.request;

import com.unitd.frame.sso.web.waf.WafHelper;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.util.HashMap;
import java.util.Map;

/**
 * @desc Request请求过滤包装即waf 请求包装
 * @filename WafRequestWrapper.java
 * @copyright www.pactera.com.cn
 * @author Hudan
 * @version 1.0
 * @date 2016/10/27
 */
public class WafRequestWrapper extends HttpServletRequestWrapper {

	private boolean filterXSS = true; 					// 对XSS攻击进行过滤
	private boolean filterSQL = true; 					// 对SQL注入攻击进行过滤

	public WafRequestWrapper(HttpServletRequest request, boolean filterXSS, boolean filterSQL) {
		super(request);
		this.filterXSS = filterXSS;
		this.filterSQL = filterSQL;
	}

	public WafRequestWrapper(HttpServletRequest request) {
		this(request, true, true);
	}

	/**
	 * @desc 数组参数过滤
	 * @param parameter 过滤参数
	 * @return 过滤后的请求数组对象值
	 */
	@Override
	public String[] getParameterValues(String parameter) {
		String[] values = super.getParameterValues(parameter);
		if (values == null) {
			return null;
		}

		int count = values.length;
		String[] encodedValues = new String[count];
		for (int i = 0; i < count; i++) {
			encodedValues[i] = filterParamString(values[i]);
		}

		return encodedValues;
	}

	/**
	 * @desc map参数过滤
	 * @return 过滤后的请求数组对象值
	 */
	@Override
	@SuppressWarnings({"rawtypes", "unchecked"})
	public Map getParameterMap() {
		Map<String, String[]> primary = super.getParameterMap();
		Map<String, String[]> result = new HashMap<>(primary.size());
		for (Map.Entry<String, String[]> entry : primary.entrySet()) {
			result.put(entry.getKey(), filterEntryString(entry.getValue()));
		}
		return result;
	}

	/**
	 * @desc 对请求数组对象进行过滤
	 * @param rawValues 需要过滤的数组值
	 * @return 过滤后的数组值
	 */
	protected String[] filterEntryString(String[] rawValues) {
		for (int i = 0; i < rawValues.length; i++) {
			rawValues[i] = filterParamString(rawValues[i]);
		}
		return rawValues;
	}

	/**
	 * @desc 参数过滤
	 * @param parameter 过滤参数
	 * @return 过滤后的请求参数
	 */
	@Override
	public String getParameter(String parameter) {
		return filterParamString(super.getParameter(parameter));
	}

	/**
	 * @param name 过滤内容
	 * @return 过滤后的请求头
	 * @desc 请求头过滤
	 */
	@Override
	public String getHeader(String name) {
		return filterParamString(super.getHeader(name));
	}

	/**
	 * @return 过滤后的Cookie
	 * @desc Cookie内容过滤
	 */
	@Override
	public Cookie[] getCookies() {
		Cookie[] existingCookies = super.getCookies();
		if (existingCookies != null) {
			for(Cookie cookie : existingCookies) {
				cookie.setValue(filterParamString(cookie.getValue()));
			}
		}
		return existingCookies;
	}

	/**
	 * @desc 过滤字符串内容
	 * @param rawValue 待处理内容
	 * @return 过滤后的字符串内容
	 */
	protected String filterParamString(String rawValue) {
		if (rawValue == null) {
			return null;
		}
		String tmpStr = rawValue;
		if (this.filterXSS) {
			tmpStr = WafHelper.stripXSS(rawValue);
		}
		if (this.filterSQL) {
			tmpStr = WafHelper.stripSqlInjection(tmpStr);
		}
		return tmpStr;
	}
}